import 'package:get/get.dart';
import 'package:kkhousekeep/models/notice_detail_entity.dart';
import 'package:kkhousekeep/models/notice_list_entity.dart';
import 'package:kkhousekeep/net/home_notice_connect.dart';

import '../../../../tools/shared_preferences.dart';
import '../../../login/login_controller.dart';

class NoticeDetailController extends GetxController {
  int id = 0;
  var title = "".obs;
  var content = "".obs;
  NoticeListObjList? _noticeModel;
  late HomeNoticeConnect _homeNoticeConnect;

  @override
  void onInit() {
    if (!Get.isRegistered<HomeNoticeConnect>()) {
      _homeNoticeConnect = Get.put(HomeNoticeConnect());
    } else {
      _homeNoticeConnect = Get.find<HomeNoticeConnect>();
    }
    _noticeModel = Get.arguments;
    id = _noticeModel?.id ?? 0;
    title.value = _noticeModel?.title ?? "";
    content.value = _noticeModel?.context ?? "";
    super.onInit();
    _loadNetworkData();
  }

  Future<void> _loadNetworkData() async {
    UserType userType = await getSp().getUserType();
    NoticeDetailEntity noticeListEntity;
    noticeListEntity = await _homeNoticeConnect.getNoticeDetail(
        id: id, isOwner: userType == UserType.owner);
    NoticeDetailObj noticeDetailObj = noticeListEntity.obj;
    title.value = noticeDetailObj.title;
    content.value = noticeDetailObj.context;
  }
}
